package com.xiaoy.util.bean;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.PropertyUtilsBean;

public class BeanUtils {
	
	/**
	 * 将map转换成javaBean
	 * @param clazz
	 * @param map
	 * @return
	 * 转换日期格式的时候有问题
	 */
	public static <T> T mapToBean(Class<T> clazz, Map<String, Object> map) {
		T obj = null;
		try {
			obj = clazz.newInstance();
			BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
			PropertyDescriptor[] descriptors = beanInfo
					.getPropertyDescriptors();
			for (int i = 0; i < descriptors.length; i++) {
				PropertyDescriptor descriptor = descriptors[i];
				String propertyName = descriptor.getName();
				if (map.containsKey(propertyName)) {
					Object value = map.get(propertyName);
					Object[] args = new Object[1];
					args[0] = value;
					descriptor.getWriteMethod().invoke(obj, args);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}
	
	/**
	 * JavaBean转Map   
	 * @param obj
	 * @return
	 */ 
	public static Map<String, Object> beanToMap(Object obj) {
		Map<String, Object> params = new HashMap<String, Object>(0);
		try {
			PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
			PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
			for (int i = 0; i < descriptors.length; i++) {
				String name = descriptors[i].getName();
				if (!"class".equals(name)) {
					params.put(name, propertyUtilsBean.getNestedProperty(obj,
							name));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return params;
	}
	
	public static <T> T beanCopy (Object resource,Class<T>returnClazz) {
		PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
		
//		T result = null;
//		try {
//			result = returnClazz.newInstance();
//			
//			PropertyDescriptor[] result_descriptors = propertyUtilsBean.getPropertyDescriptors(result);
//			PropertyDescriptor[] resource_descriptors = propertyUtilsBean.getPropertyDescriptors(resource);
//			
//			for (int i = 0; i < result_descriptors.length; i++) {
//				String name = result_descriptors[i].getName();
//				PropertyDescriptor descriptor = result_descriptors[i];
//				if (!"class".equals(name)) {
//					for (PropertyDescriptor pd:resource_descriptors) {
//						if (pd.getName()!=null&&pd.getName().equals(name)){
//							
//							Object value = propertyUtilsBean.getNestedProperty(resource,name);
//							Object[] args = new Object[1];
//							args[0] = value;
//							descriptor.getWriteMethod().invoke(result, args);
//							break;
//						}
//					}
//				}
//			}
//			
//		} catch (InstantiationException e) {
//			e.printStackTrace();
//		} catch (IllegalAccessException e) {
//			e.printStackTrace();
//		} catch (InvocationTargetException e) {
//			e.printStackTrace();
//		} catch (NoSuchMethodException e) {
//			e.printStackTrace();
//		}
		
		T result = null;
		try {
			result = returnClazz.newInstance();
			beanCopy(resource, result);
			
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static void beanCopy (Object resource,Object target) {
		
		PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
		try {
			PropertyDescriptor[] result_descriptors = propertyUtilsBean.getPropertyDescriptors(target);
			PropertyDescriptor[] resource_descriptors = propertyUtilsBean.getPropertyDescriptors(resource);
			
			for (int i = 0; i < result_descriptors.length; i++) {
				String name = result_descriptors[i].getName();
				PropertyDescriptor descriptor = result_descriptors[i];
				if (!"class".equals(name)) {
					for (PropertyDescriptor pd:resource_descriptors) {
						if (pd.getName()!=null&&pd.getName().equals(name)){
							
							Object value = propertyUtilsBean.getNestedProperty(resource,name);
							if(value == null) {
								continue;
							}
							Object[] args = new Object[1];
							args[0] = value;
							descriptor.getWriteMethod().invoke(target, args);
							break;
						}
					}
				}
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
	
	
	
}


